/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package robotlion.view;

import java.awt.Point;
import java.util.Iterator;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;
import robotlion.model.EEtat;

/**
 *
 * @author test
 */
public class ListeRobotTableModel extends DefaultTableModel {

    public ListeRobotTableModel() {
        Object[] tableColumnNames = new Object[2];
        tableColumnNames[0] = "Robot";
        tableColumnNames[1] = "Etat";
        this.setColumnIdentifiers(tableColumnNames);        
    }
    
    @Override
    public boolean isCellEditable(int row, int column) {
        return false;
    }
    
    public void update(String nomRobot, EEtat etat, Point position)
    {
        Vector theRow = this.getRowByRobotName(nomRobot);
        int rowNum;
        String setat = "";
        switch(etat)
        {
            case Libre:
                setat = "Libre ["+position.x+","+position.y+"]";
                break;
            case EteindUnIncendie:
                setat = "Eteind un icendie ["+position.x+","+position.y+"]";
                break;
            case SeDeplace:
                setat = "Se déplace ["+position.x+","+position.y+"]";
                break;
        }
            
        if(theRow != null)
        {
            theRow.setElementAt(etat, 1);
            rowNum = this.getDataVector().indexOf(theRow);
            this.fireTableRowsUpdated(rowNum, rowNum);
        }
        else
        {
            theRow = new Vector();
            theRow.add(0, nomRobot);
            theRow.add(1, etat);
            this.addRow(theRow);
            rowNum = this.getDataVector().indexOf(theRow);
            this.fireTableRowsInserted(rowNum, rowNum);
        }
    }
    
    public String getNomRobot(int rowNumber)
    {
        return (String)this.getValueAt(rowNumber, 0);
    }
    
    public Vector getRowByRobotName(String robotName)
    {
        Vector listeRobot = this.getDataVector();
        Iterator it = listeRobot.iterator();
        while(it.hasNext())
        {
            Vector row = (Vector)it.next();
            if(((String)row.get(0)).equals(robotName))
            {
                return row;
            }
        }
        return null;
    }
    
    public void deleteRobotByName(String robotName)
    {
        Vector theRow = this.getRowByRobotName(robotName);
        int rowNum = this.getDataVector().indexOf(theRow);
        this.removeRow(rowNum);
    }
}
